﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
 */
 
import asgard.config.Config;
import asgard.events.RemotingEvent;
import asgard.net.remoting.RemotingService;

import mars.core.ApplicationCommand;

import pegas.events.ActionEvent;

import vegas.events.Delegate;
import vegas.events.EventListener;
import vegas.string.StringFormatter;
import vegas.util.ConstructorUtil;

/**
 * The Application remoting service.
 * @author eKameleon
 */
class egallery.net.remoting.ApplicationService extends RemotingService 
{

	/**
	 * Creates a new ApplicationService instance
	 */
	function ApplicationService() 
	{
		
		super();
		
		_errorListener   = new Delegate( this , onError   ) ;
		_faultListener   = new Delegate( this , onFault   ) ;
		_finishListener  = new Delegate( this , onFinish  ) ;
		_startListener   = new Delegate( this , onStart   ) ;
		_timeoutListener = new Delegate( this , onTimeout ) ;
		
		registerEventListener() ;
		
	}
	
	/**
	 * The id in the config of the application of all services setings.
	 */
	public static var CONFIG_SERVICE_ID:String = "services" ;
	
	/**
	 * Clear the service.
	 */
	public function clear():Void
	{
		gatewayUrl = null ;
		serviceName = null ;
		methodName  = null ;
		params = null ;
	}
	
	/**
	 * Returns the gateway url of the remoting server.
	 * @return the gateway url of the remoting server.
	 */
	public static function getConfigGatewayUrl():String
	{
		var config:Config = Config.getInstance() ;
		return (new StringFormatter( config.gatewayUrl )).format( config.httphost ) ;	
	}

	/**
	 * Returns the method name in the config of the specified id in argument.
	 * @return the method name in the config of the specified id in argument.
	 */
	public static function getConfigMethodName( id ):String
	{
		return Config.getInstance()[CONFIG_SERVICE_ID][id].methodName || null ;
	}
		
	/**
	 * Returns the service name in the config of the specified id in argument.
	 * @return the service name in the config of the specified id in argument.
	 */
	public static function getConfigServiceName( id ):String
	{
		return Config.getInstance()[CONFIG_SERVICE_ID][id].serviceName || null ;
	}

	/**
	 * Invoqued if the Remoting connection is failed
	 */
	public function onError(e:RemotingEvent):Void
	{
		getLogger().error(ConstructorUtil.getName(this) + " : " + e.getType() + " - " + e.code) ;
	}

	/**
	 * Invoqued if the Remoting connection is finish.
	 */
	public function onFinish(e:ActionEvent):Void
	{
		// getLogger().debug(ConstructorUtil.getName(this) + " : " + e.getType()) ;
		ApplicationCommand.unprotectScreen() ;
	}

	/**
	 * Invoqued if the Remoting connection receive a fault.
	 */
	public function onFault(e:RemotingEvent):Void
	{
		getLogger().error(this + " : " + e.type + " : " + e.getCode() + " :: " + e.getDescription()) ;
	}

	/**
	 * Invoqued if the Remoting connection start.
	 */		
	public function onStart(e:ActionEvent):Void
	{
		getLogger().debug(ConstructorUtil.getName(this) + " : " + e.type ) ;
		ApplicationCommand.protectScreen() ;
	}

	/**
	 * Invoqued if the Remoting connection is time out.
	 */		
	public function onTimeout(e:RemotingEvent):Void
	{
		getLogger().warn(ConstructorUtil.getName(this) + " : " + e.type ) ;
	}
	
	/**
	 * Register the internal event listeners.
	 */
	public function registerEventListener():Void
	{
		addEventListener(RemotingEvent.ERROR      , _errorListener   ) ;
		addEventListener(RemotingEvent.FAULT      , _faultListener   ) ;
		addEventListener(RemotingEvent.FINISH   , _finishListener  ) ;
		addEventListener(RemotingEvent.START    , _startListener   ) ;	
		addEventListener(RemotingEvent.TIMEOUT  , _timeoutListener ) ;	
	}

	/**
	 * Unregister the internal event listeners.
	 */
	public function unregisterEventListener():Void
	{
		removeEventListener(RemotingEvent.ERROR      , _errorListener   ) ;
		removeEventListener(RemotingEvent.FAULT      , _faultListener   ) ;
		removeEventListener(RemotingEvent.FINISH   , _finishListener  ) ;
		removeEventListener(RemotingEvent.START    , _startListener   ) ;	
		removeEventListener(RemotingEvent.TIMEOUT  , _timeoutListener ) ;	
	}

	private var _errorListener:EventListener  ;
	
	private var _faultListener:EventListener  ;
	
	private var _finishListener:EventListener ;
	
	private var _startListener:EventListener  ;
	
	private var _timeoutListener:EventListener ;

}